package com.weishu.controller;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.domain.AlipayTradeWapPayModel;
import com.alipay.api.request.AlipayTradeWapPayRequest;
import com.weishu.config.AliPayConfig;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Random;

/**
 * 作者: liyan
 * 日期: 2021/8/3 10:35
 * 描述:
 */

@RestController
public class ShouJiWangZhanPayController {

    @RequestMapping(value = "shoujiPay",produces = "text/html;charset=utf-8")
    public String shoujiPay(){
        // 商户订单号，商户网站订单系统中唯一订单号，必填
        String out_trade_no ="6823789339978248"+new Random().nextInt(10000);
        // 订单名称，必填
        String subject = "手机网站支付";
        // 付款金额，必填
        String total_amount="100";
        // 商品描述，可空(这个信息会出现在支付宝支付详情的备注里)
        String body = "这是手机网站的商品支付";
        // 超时时间 可空  2m表示两分钟
        String timeout_express="2m";
        // 销售产品码 必填
        String product_code="FACE_TO_FACE_PAYMENT";
        /**********************/
        // SDK 公共请求类，包含公共请求参数，以及封装了签名与验签，开发者无需关注签名与验签
        //调用RSA签名方式
        AlipayClient client = new DefaultAlipayClient(AliPayConfig.aliServiceUrl, AliPayConfig.APP_ID,
                AliPayConfig.APP_PRIVATE_KEY,"JSON","utf-8", AliPayConfig.ALIPAY_PUBLIC_KEY,"RSA2");

        AlipayTradeWapPayRequest alipay_request=new AlipayTradeWapPayRequest();

        // 封装请求支付信息
        AlipayTradeWapPayModel model=new AlipayTradeWapPayModel();
        model.setOutTradeNo(out_trade_no);
        model.setSubject(subject);
        model.setTotalAmount(total_amount);
        model.setBody(body);
        model.setTimeoutExpress(timeout_express);
        model.setProductCode(product_code);
        alipay_request.setBizModel(model);
        // 设置异步通知地址
        alipay_request.setReturnUrl("https://www.baidu.com");
        // 设置同步地址
        alipay_request.setReturnUrl("https://www.tmall.com");

        // form表单生产
        String form = "";
        try {
            // 调用SDK生成表单
            form = client.pageExecute(alipay_request).getBody();
            System.out.println(form);
        } catch (AlipayApiException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return form;
    }

}
